import tactu5.*;
import tactu5.Tactu5SimpleSequencer;

//ClientOSC client;
ClientMidi clientMidi;
UI ui;

// creating an instance of Tactu5
Tactu5 tactu5;

// creating an aggregator instance
Aggregator aggregator;

Sequence sequence1 = new Sequence();
Sequence sequence2 = new Sequence();



public int maxNotes = 8;
public void maxNotes(int v) 
{
    maxNotes = v;
}

public int mainDuration = 150;
public void mainDuration(int v) 
{
    mainDuration = v;
}

public int silenceProb = 2;
public void silenceProb(int v) 
{
    silenceProb = v;
}


boolean inited = false;
int countNotes = 0;
void setup() 
{
  size(700,400);
  background(150);
  //client = new ClientOSC();
  clientMidi = new ClientMidi(this);
  ui = new UI(this);
  
  addSeq();
}





void mousePressed()
{
  //addSeq();
}


void addSeq()
{
    
    sequence1=new Sequence();
    sequence2=new Sequence();
    
    
  
    // declaring some notes
    for(int i=0; i<maxNotes; i++)
    {
      String name1 = "m" + int(random(6,23));
      int dur1 = mainDuration;
      int rnd = int(random(0,silenceProb));
      Note note1;
      if(rnd==0)
      {
        note1 = new Note(dur1, false, name1);
      }else{
        note1 = new Note(dur1, false, "nosound");
      }
  
      sequence1.addNote(note1);
    }
  
    for(int i=0; i<maxNotes/4; i++)
    {
      String name2 = "m" + int(random(0,5));
      int dur2 = mainDuration*4;
      Note note2 = new Note(dur2, false, name2);
      sequence2.addNote(note2);
    }
  
    //aggregator.resetAll();
    aggregator = new Aggregator();
    aggregator.addSequence(sequence1);
    aggregator.addSequence(sequence2);
  
  
    if(inited)
    {
        tactu5.updateSequencer(aggregator.getScore());
    }else{
        tactu5 = new Tactu5(this,aggregator.getScore(),true);
        tactu5.start();     
    }
    
    inited = true;
}

void draw() 
{
}

void noteReciver(Note n)
{
  countNotes++;
  if(countNotes >= maxNotes*4)
  {
    println("CHANGE!");
    countNotes = 0;
    addSeq();
    //return;
  }
  
  //client.send(n.getComment(), 0);
  //client.send(n.getComment(), 1); 
  clientMidi.send();
  

}
void stop() 
{
  tactu5.closeSequencer();
  super.stop();
}

